package com.ruoyi.system.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.EmployeeMapper;
import com.ruoyi.system.mapper.RewardsPunishmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HistoryRpMapper;
import com.ruoyi.system.service.IHistoryRpService;
import com.ruoyi.common.core.text.Convert;

/**
 * 历史考勤Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-14
 */
@Service
public class HistoryRpServiceImpl implements IHistoryRpService 
{
    @Autowired
    private HistoryRpMapper historyRpMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RewardsPunishmentMapper rewardsPunishmentMapper;

    /**
     * 查询历史考勤
     * 
     * @param historyrpId 历史考勤主键
     * @return 历史考勤
     */
    @Override
    public HistoryRp selectHistoryRpByHistoryrpId(Long historyrpId)
    {
        return historyRpMapper.selectHistoryRpByHistoryrpId(historyrpId);
    }

    /**
     * 查询历史考勤列表
     * 
     * @param historyRp 历史考勤
     * @return 历史考勤
     */
    @Override
    @DataScope(deptAlias = "e", userAlias = "su")
    public List<HistoryRp> selectHistoryRpList(HistoryRp historyRp)
    {
        return historyRpMapper.selectHistoryRpList(historyRp);
    }

    /**
     * 新增历史考勤
     * 
     * @param historyRp 历史考勤
     * @return 结果
     */
    @Override
    public int insertHistoryRp(HistoryRp historyRp)
    {
        historyRp.setCreateTime(DateUtils.getNowDate());
        return historyRpMapper.insertHistoryRp(historyRp);
    }

    /**
     * 修改历史考勤
     * 
     * @param historyRp 历史考勤
     * @return 结果
     */
    @Override
    public int updateHistoryRp(HistoryRp historyRp)
    {
        historyRp.setUpdateTime(DateUtils.getNowDate());
        return historyRpMapper.updateHistoryRp(historyRp);
    }

    /**
     * 批量删除历史考勤
     * 
     * @param historyrpIds 需要删除的历史考勤主键
     * @return 结果
     */
    @Override
    public int deleteHistoryRpByHistoryrpIds(String historyrpIds)
    {
        return historyRpMapper.deleteHistoryRpByHistoryrpIds(Convert.toStrArray(historyrpIds));
    }

    /**
     * 删除历史考勤信息
     * 
     * @param historyrpId 历史考勤主键
     * @return 结果
     */
    @Override
    public int deleteHistoryRpByHistoryrpId(Long historyrpId)
    {
        return historyRpMapper.deleteHistoryRpByHistoryrpId(historyrpId);
    }

    @Override
    @Scheduled(cron = "0 0 23 ? * MON-FRI")//周内23点保存 "0 0 23 ? * MON-FRI"
    public void saveHistoryRp() {
        List<Long> employeeId = employeeMapper.selectAllEmployeeId();
        for(Long id : employeeId){
            RewardsPunishment rewardsPunishment = rewardsPunishmentMapper.selectRpByEmployeeId(id);
            HistoryRp historyRp = new HistoryRp();
            historyRp.setEmployeeId(rewardsPunishment.getEmployeeId());
            historyRp.setEmployeeName(rewardsPunishment.getEmployeeName());
            Employee employee = employeeMapper.selectEmployeeByEmployeeId(id);
            Date signtime = employee.getSignTime();
            Calendar ontime = Calendar.getInstance();
            ontime.set(2000, Calendar.JANUARY, 1, 8, 0);
            Date ontimes = ontime.getTime();
            System.out.println(ontimes);
            System.out.println(signtime);
            int compareTo = ontimes.compareTo(signtime);
            if(compareTo == 1){
                historyRp.setIsLeave("是");
                historyRp.setCreateTime(new Date());
                historyRpMapper.insertHistoryRp(historyRp);
                continue;
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(signtime);
            int weekday = calendar.get(Calendar.DAY_OF_WEEK);
            if(weekday >= 2 && weekday <= 7) weekday--;
            else weekday = 7;
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            if(weekday <= 5){//周内
                if(hour < 9){//未超过九点,不迟到
                    historyRp.setLate("否");
                    historyRp.setAttendance("否");
                }
                else if(hour < 14) {
                    historyRp.setLate("是");
                    historyRp.setAttendance("否");
                } else {
                    historyRp.setAttendance("是");
                    historyRp.setLate("是");
                }
            }
            historyRp.setCreateTime(new Date());
            historyRpMapper.insertHistoryRp(historyRp);
        }
    }
}
